/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 jfinal app. jfapp Group.
 */

package app.models.game;

import com.github.sog.annotation.TableBind;
import com.github.sog.config.StringPool;
import com.github.sog.controller.datatables.core.ColumnDef;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.google.common.base.Strings;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * The table ol_game_awards mapping model.
 * </p>
 */
@TableBind(tableName = "ol_game_awards")
public class GameAwards extends Model<GameAwards> {

    private static final long   serialVersionUID = 1L;
    /**
     * The public dao.
     */
    public static final GameAwards dao = new GameAwards();


    public List<GameAwards> list(){
    	return find(SqlKit.sql("gameawards.list"));
    }

    public Page<GameAwards> datatable(DatatablesCriterias criterias) {
        int pageSize = criterias.getDisplaySize();
        int start = criterias.getDisplayStart() / pageSize + 1;
        final List<ColumnDef> sortingColumnDefs = criterias.getSortingColumnDefs();
        String where = " FROM ol_game_awards, ol_game_gift WHERE ol_game_awards.gift=ol_game_gift.id";
        if (sortingColumnDefs != null && !sortingColumnDefs.isEmpty()) {
            StringBuilder orderBy = new StringBuilder();
            for (ColumnDef sortingColumnDef : sortingColumnDefs)
                if (sortingColumnDef.isSortable()) {
                    orderBy.append(sortingColumnDef.getName()).append(StringPool.SPACE).append(sortingColumnDef.getSortDirection().toString());
                }
            // todo mulite column.
            final String byColumns = orderBy.toString();
            if(!Strings.isNullOrEmpty(byColumns)){
                where += " ORDER BY " + byColumns;
            }
        }
        if (!where.contains("ORDER")) {
            where += " ORDER BY ol_game_awards.size";
        }

        return paginate(start, pageSize, SqlKit.sql("gameawards.columns"), where);
    }

    public List<GameAwards> findByGameId(int gameId) {
        return find(SqlKit.sql("gameawards.findByGameId"), gameId);
    }

    public List<Record> findWithGiftByGameId(int gameId) {
        return Db.find(SqlKit.sql("gameawards.findWithGiftByGameId"), gameId);
    }

    public List<Record> findBalSizeWithGiftByGameId(int gameId) {
        List<Record> records = Db.find(SqlKit.sql("gameawards.findBalSizeWithGiftByGameId"), gameId, gameId, gameId);
        if (records != null) {
            Iterator<Record> iterator = records.iterator();
            while (iterator.hasNext()) {
                Record r = iterator.next();
                Boolean state = r.getBoolean("awardsState");
                if (state != null && state == true) {
                    iterator.remove();
                    continue;
                }
                int awardsSize = r.getInt("awardsSize");
                long recordAwardsSize = r.getNumber("recordAwardsSize") == null ? 0 : r.getNumber("recordAwardsSize").longValue();
                if (recordAwardsSize < awardsSize) {
                    continue;
                } else {
                    iterator.remove();
                }
            }
        }
        return records;
    }
}